from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
import win32api,win32con

#  chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\selenum\AutomationProfile"  (打开可调式的浏览器)
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
chrome_driver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
driver = webdriver.Chrome(chrome_driver, options=chrome_options)
# driver.get("https://www.baidu.com/")

for i in range(10000000):
    d = "无未找到容量"
    a=-1
    try:
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        d=driver.find_element_by_xpath('//*[@id="collapseThree"]/div/div/div/table/tbody/tr/td[6]/span').get_attribute('outerHTML')
        if '未找到' in d:
            try:
                d=driver.find_element_by_xpath('//*[@id="collapseThree"]/div/div/div/table/tbody/tr[0]/td[6]/span').get_attribute('outerHTML')
                a=0
            except:
                print(0)
        if '无' in d:
            try:
                d=driver.find_element_by_xpath('//*[@id="collapseThree"]/div/div/div/table/tbody/tr[1]/td[6]/span').get_attribute('outerHTML')
                a=1
            except:
                print(1)
        if '无' in d:
            try:
                d=driver.find_element_by_xpath('//*[@id="collapseThree"]/div/div/div/table/tbody/tr[2]/td[6]/span').get_attribute('outerHTML')
                a=2
            except:
                print(2)
    except:
        driver.refresh()
        time.sleep(1)
    if '有' in d:
        if a==-1:
            try:
                driver.find_element_by_xpath(
                    '//*[@id="collapseThree"]/div/div/div/table/tbody/tr/td[1]/div/label/span[3]').click()
                # time.sleep(1)
                driver.find_element_by_xpath('//*[@id="btnSearch"]').click()
                print("成功")
                win32api.MessageBox(0, "成功", "提醒", win32con.MB_OK)
            except:
                driver.execute_script("window.scrollBy(0,100)")
                time.sleep(1)
                print("有但是出错")
        elif a==0:
            try:
                driver.find_element_by_xpath(
                    '//*[@id="collapseThree"]/div/div/div/table/tbody/tr[0]/td[1]/div/label/span[3]').click()
                # time.sleep(1)
                driver.find_element_by_xpath('//*[@id="btnSearch"]').click()
                print("成功")
                win32api.MessageBox(0, "成功", "提醒", win32con.MB_OK)
            except:
                driver.execute_script("window.scrollBy(0,100)")
                time.sleep(1)
                print("有但是出错")
        elif a == 1:
            try:
                driver.find_element_by_xpath(
                    '//*[@id="collapseThree"]/div/div/div/table/tbody/tr[1]/td[1]/div/label/span[3]').click()
                # time.sleep(1)
                driver.find_element_by_xpath('//*[@id="btnSearch"]').click()
                print("成功")
                win32api.MessageBox(0, "成功", "提醒", win32con.MB_OK)
            except:
                driver.execute_script("window.scrollBy(0,100)")
                time.sleep(1)
                print("有但是出错")
        elif a == 2:
            try:
                driver.find_element_by_xpath(
                    '//*[@id="collapseThree"]/div/div/div/table/tbody/tr[2]/td[1]/div/label/span[3]').click()
                # time.sleep(1)
                driver.find_element_by_xpath('//*[@id="btnSearch"]').click()
                print("成功")
                win32api.MessageBox(0, "成功", "提醒", win32con.MB_OK)
            except:
                driver.execute_script("window.scrollBy(0,100)")
                time.sleep(1)
                print("有但是出错")

    else:
        print(i,d)

    try:
        driver.refresh()  # 刷新方法 refresh
        print('test pass: refresh successful')
    except Exception as e:
        print("Exception found", format(e))
    time.sleep(1)
# d=driver.find_element_by_xpath('//*[@id="collapseThree"]/div/div/div/table/tbody/tr/td[6]/span').get_attribute('outerHTML')
# if '有' in d:
#     driver.find_element_by_xpath('//*[@id="collapseThree"]/div/div/div/table/tbody/tr/td[1]/div/label/span[3]').click()



































































#     driver.find_element_by_xpath('//*[@id="btnSearch"]').click()
#     print("成功")
# else:
#     print(d)